package com.jxltech.krpano.api.util;

import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.util.List;

import static com.jxltech.krpano.api.util.Constants.VR_FILE_NAME;

/**
 * Created by feng on 2020/8/30 0030
 */
@Slf4j
public class VelocityUtils {

	/**
	 * 设置模板变量信息
	 *
	 * @return 模板列表
	 */
	public static VelocityContext prepareContext(SceneBase sceneBase, String absolutePath, String relativePath) {

		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("sceneBase", sceneBase);

		//music
		setMusicContext(velocityContext, absolutePath + "/music", relativePath + "/music/");

		//video
		setVideoContext(velocityContext, absolutePath + "/video", relativePath + "/video/");

		//scene
		try {
			setSceneListContext(velocityContext, absolutePath + "/panos", relativePath + "/panos/");

		}catch (Throwable e){
			e.printStackTrace();
			throw e;
		}

		return velocityContext;
	}

	private static void setVideoContext(VelocityContext velocityContext, String absolutePath, String relativePath) {
		Video video = new Video();
		File videoDir = new File(absolutePath);
		if (videoDir.listFiles() != null && videoDir.listFiles().length > 0) {
			for (File vFile : videoDir.listFiles()) {
				String filePath = relativePath +vFile.getName();
				if(vFile.getName().endsWith(Constants.VIDEO_MP4)) video.setMp4(filePath);
				if(vFile.getName().endsWith(Constants.VIDEO_WEBM)) video.setWebm(filePath);
				if(vFile.getName().endsWith(Constants.VIDEO_JPG)) video.setJpg(filePath);
			}
		}
		velocityContext.put("video", video);
	}

	private static void setMusicContext(VelocityContext velocityContext, String absolutePath, String relativePath) {
		String backgroundmusic = "/static/music/backgroundmusic.mp3";
		File musicDir = new File(absolutePath);
		if (musicDir.listFiles() != null && musicDir.listFiles().length > 0) {
			backgroundmusic = relativePath + musicDir.listFiles()[0].getName();
		}
		velocityContext.put("backgroundmusic", backgroundmusic);
	}

	/**
	 * 获取模板信息
	 *
	 * @return 模板列表
	 */
	public static String getTemplate() {
		return "vm/tour.xml.vm";
	}

	/**
	 * @param velocityContext
	 * @param panosPath             user panos 文件夹位置
	 * @param envRelativeStaticPath 全景图相对static文件夹的位置 例如： myid/panos
	 */
	private static void setSceneListContext(VelocityContext velocityContext, String panosPath, String envRelativeStaticPath) {
		List<Scene> sceneList = Lists.newArrayList();
		velocityContext.put("sceneList", sceneList);
		if(!FileUtil.exist(panosPath)) return;
		//根据path路径获取的 path的 文件夹列表
		for (String fileName : new File(panosPath).list()) {
			//遍历每个文件夹设置scene
			File tilesDir = FileUtil.newFile(panosPath + File.separator + fileName);
			if (!tilesDir.isDirectory()) continue;
			Scene scene = new Scene();
			scene.setTilesPath(envRelativeStaticPath + fileName);
			String sceneName = fileName.replace(".tiles", "");
			scene.setName("scene_" + sceneName);
			scene.setTitle(sceneName);
			for (File file : tilesDir.listFiles()) {
				if (!file.getName().equalsIgnoreCase(VR_FILE_NAME) && file.isDirectory()) {
					scene.setImageSize(file.listFiles().length);
					break;
				}
			}
			sceneList.add(scene);
		}

	}
}
